package com.tju.resource.business.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.resource.annotation.Log;
import com.tju.resource.common.ServiceException;
import com.tju.resource.mapper.SysParamsMapper;
import com.tju.resource.model.SysParams;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.enum_utils.FunctionType;
import com.tju.resource.tool.tool.ChineseExceptionUtils;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.tool.tool.EmptyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统参数
 */
@Service
public class SysParamsServiceImpl implements SysParamsService {

    @Autowired
    SysParamsMapper mapper;
    @Autowired
    ChineseExceptionUtils zh_CH;

    @Override
    public SysParams get(String key) {
        QueryWrapper<SysParams> query = new QueryWrapper<>();
        query.eq("params_key", key);
        return mapper.selectOne(query);
    }

    @Override
    public Map<String, SysParams> getAll(Object... keys) {
        QueryWrapper<SysParams> query = new QueryWrapper<>();
        List<SysParams> qResult;
        Map<String, SysParams> result;
        query.in("params_key", keys);
        qResult = mapper.selectList(query);
        result = new HashMap<>();
        qResult.forEach(pa -> result.put(pa.getParamsKey(), pa));
        return result;
    }

    @Override
    public Result<Object> add(SysParams params) {
        if (null != params.getParamsKey() && !StringUtils.equals(params.getParamsKey(), "")) {
            SysParams sysParams = get(params.getParamsKey());
            if (null == sysParams) {
                mapper.insert(params);
                return ResultUtil.success(ResultEnum.SUCCESS);
            } else {
                throw new ServiceException("参数键(key)不可重复!");
            }
        } else {
            throw new ServiceException("参数值(value)不可为空!");
        }
    }

    /**
     * 分页查询全部系统参数信息
     *
     * @param parameters
     * @return
     */
    @Override
    public Result<Object> getPageList(ClientParameters parameters) {
        try {
            QueryWrapper<SysParams> wrapper = new QueryWrapper<>();
            if (EmptyUtils.isNotEmpty(parameters.getOne())) {
                wrapper.like("params_key", parameters.getOne());
            }
            if (EmptyUtils.isNotEmpty(parameters.getPage()) && EmptyUtils.isNotEmpty(parameters.getLimit())) {
                Page<SysParams> page = new Page<>(parameters.getPage(), parameters.getLimit());
                IPage<SysParams> iPage = mapper.selectPage(page, wrapper);
                return ResultUtil.success(ResultEnum.SUCCESS, iPage.getRecords(), iPage.getTotal());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /**
     * 修改系统参数信息
     *
     * @param sysParams
     * @return
     */
    @Override
    @Transactional
    @Log(type = FunctionType.Modify)
    public Result<Object> updateParams(SysParams sysParams) {
        try {
            if (EmptyUtils.isNotEmpty(sysParams.getId())) {
                if (EmptyUtils.isNotEmpty(sysParams.getParamsValue())) {
                    if (mapper.updateById(sysParams) > 0) {
                        return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.SuccessfullyModified);
                    }
                } else
                    return ResultUtil.error(ResultEnum.UNKONW_ERROR, "请输入参数值");
            } else
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.MissingParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.FailToEdit);
    }
}
